/*
 * set dashboard portlet
 */
Qlsolution.widgets.portlet.start({
	selector: '.widgetPortlet:eq(0) .column',
	placeholder: 'widgetPortletPlaceholder br7',
	connectWith: '.widgetPortlet:eq(0) .column',
	columnWidth: '100%',
	selectorElement: '.widgetPortlet:eq(0) .portlet'
});

Qlsolution.widgets.portlet.start({
	selector: '.widgetPortlet:eq(1) .column',
	placeholder: 'widgetPortletPlaceholder br7',
	connectWith: '.widgetPortlet:eq(1) .column',
	columnWidth: '50% 50%',
	selectorElement: '.widgetPortlet:eq(1) .portlet'
});


/*
 * set controller
 */
Qlsolution.controllers.timesheet = {
	store: {
		saveTimesheet: 'index.php/timesheets/saveTimesheet',
		getTimesheet: 'index.php/timesheets/getTimesheet',
		getProjects: 'index.php/projects/getProjects'
	},

	layoutSelector:{
		projectsList: '.templateTimesheet .projectsList',
		timesheetList: '.templateTimesheet .timesheetList form'
	},

	projects: new Object,

	/*
	 * get projects
	 */
	getProjects: function(){
		$.ajax({
			type: 'post',
			url: Qlsolution.controllers.timesheet.store.getProjects,
			success: function(msg){
				$(Qlsolution.controllers.timesheet.layoutSelector.projectsList).append($('<ul/>'));
				var projects = Qlsolution.controllers.timesheet.projects = $.parseJSON(msg);
				for (val in projects){
					$(Qlsolution.controllers.timesheet.layoutSelector.projectsList + ' ul').append('<li>' + projects[val].name + '</li>');
				}
				//generate timesheet
				Qlsolution.controllers.timesheet.generateTimesheet();
			}
		});
	},

	/*
	 * generate timesheet
	 */
	generateTimesheet: function(){
		var date = new Date(),
			actualMonth = date.getMonth(),
			actualYear = date.getFullYear();

		//prototype date: get month name, get day name
		Date.prototype.getMonthName = function(){
			var m = ['January','February','March','April','May','June','July','August','September','October','November','December'];
			return m[this.getMonth()];
		};
		Date.prototype.getDayName = function() {
			var d = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
			return d[this.getDay()];
		};

		var settings = {
			month: actualMonth,
			year: actualYear
		};
		Qlsolution.controllers.timesheet.generateMonth(settings);
	},

	/*
	 * generate month
	 */
	generateMonth: function(settings){
		var actualDaysInMonth = 32 - new Date(settings.year, settings.month, 32).getDate(),
			dayWidth = (100 / actualDaysInMonth) + '%',
			tempDate,
			date = new Date(),
			today = date.getDay(),
			todayCls = '';

		//set month, year in hidden input field
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' input[name="month"]').val(settings.month);
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' input[name="year"]').val(settings.year);

		//generate list
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList).remove('ul');
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList).append($('<ul/>'));
		for(var i=0; i<actualDaysInMonth; i++){
			tempDate = new Date(settings.year, settings.month, i);
			todayCls = '';
			if(today == (i+1)){
				todayCls = 'today';
			}
			$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' ul').append($('<li/>'));
			$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' ul li:last').css({
				'width': dayWidth
			}).attr({
				'class': tempDate.getDayName() + ' ' + todayCls,
				'id': 'day'+i
			});
			$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' ul li:last').append('<p class="header" title="' + tempDate.getDayName() + '">' + (i+1) + '</p>');
			for (val in Qlsolution.controllers.timesheet.projects){
				$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' ul li:last').append('<p><input type="text" class="project'+ Qlsolution.controllers.timesheet.projects[val].id_node +'" /></p>');
			}
		}

		//submit form by enter
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' input').keypress(function(event){
			if(event.which == 13){
				$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList).trigger('submit');
			}
		});

		//submit form
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList).submit(function(){
			Qlsolution.controllers.timesheet.saveTimesheet();
			return false;
		});

		//get timesheet
		Qlsolution.controllers.timesheet.getTimesheets(settings);
	},

	/*
	 * get timesheet
	 */
	getTimesheets: function(settings){
		var paramsData = new Object();
		paramsData['month'] = settings.month;
		paramsData['year'] = settings.year;
		$.ajax({
			type: 'post',
			data: 'params=' + $.toJSON(paramsData),
			url: Qlsolution.controllers.timesheet.store.getTimesheet,
			success: function(msg){
				Qlsolution.controllers.timesheet.fillMonth($.parseJSON(msg));
			}
		});
	},

	fillMonth: function(data){
		var days = $.parseJSON(data[0].data),
			day;
		for(var i=0; i<days.length; i++){
			day = days[i].day;
			for(var j=0; j<days[i].projects.length; j++){
				$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' li#day' + days[i].day + ' input.project' + days[i].projects[j].id_node).val(days[i].projects[j].time);
			}
		}
	},

	/*
	 * save timesheet
	 */
	saveTimesheet: function(){
		//object schema
		var myObj = {
			month: '7',
			year: '2011',
			days:[{
				day: 1,
				projects: [
				    {
				    	id_node: 1,
						time: 2
				    },
				    {
				    	id_node: 2,
						time: 3
				    }
				]
			}]
		};

		//create object with timesheet
		var paramsData = {
			month: $(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' input[name="month"]').val(),
			year: $(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' input[name="year"]').val(),
			days: new Array
		};

		var myDay,
			myProjects,
			myIdNode,
			myTime;
		$(Qlsolution.controllers.timesheet.layoutSelector.timesheetList + ' ul li').each(function(){
			//get day
			myDay = $(this).attr('id');
			myDay = myDay.substring(3);

			//get projects array
			myProjects = new Array();
			$(this).find('input').each(function(){
				myIdNode = $(this).attr('class');
				myIdNode = myIdNode.substring(7);
				myTime = $(this).val();
				myProjects.push({
					id_node: myIdNode,
					time: myTime
				});
			});

			//push projects to days
			paramsData.days.push({
				day: myDay,
				projects: myProjects
			});
		});

		//console.log(paramsData);
		$.ajax({
			type: 'post',
			url: Qlsolution.controllers.timesheet.store.saveTimesheet,
			data: 'params=' + $.toJSON(paramsData),
			success: function(msg){
				//console.log(msg);
			}
		});
	}


};
Qlsolution.controllers.timesheet.getProjects();


/*
 * my handlers
 */

